package com.ruoyi.mobile.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mobile.domain.User;
import com.ruoyi.mobile.mapper.UserMapper;
import com.ruoyi.mobile.model.request.QuestionaireRequest;
import com.ruoyi.mobile.model.request.UserQuestionnaireRequest;
import com.ruoyi.mobile.model.vo.QuestionContentVo;
import com.ruoyi.mobile.model.vo.QuestionaireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.UserInfoMapper;
import com.ruoyi.mobile.domain.UserInfo;
import com.ruoyi.mobile.service.IUserInfoService;
import org.springframework.transaction.annotation.Transactional;

import static java.util.Arrays.asList;

/**
 * 收集用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-06
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService 
{
    @Autowired
    public UserInfoMapper userInfoMapper;

    @Autowired
    public UserMapper userMapper;


    @Override
    public List<QuestionaireVo> selectListByUserId(QuestionaireRequest questionaireRequest) {
        List<QuestionaireVo> list = userInfoMapper.selectListByUserId(questionaireRequest.getUserId(), questionaireRequest.getName(), questionaireRequest.getStatus());
        return list;
    }


    @Override
    @Transactional
    public Integer insertUserInfo(int userId, UserQuestionnaireRequest userQuestionnaireRequest) throws JsonProcessingException {
        //先检查是否已经填写
        if (userQuestionnaireRequest.getSubType() == null) {
            userQuestionnaireRequest.setSubType(0);
        }
        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> typeNeedSubList = asList(2,4,6,8,10,12,14,16);
        if (typeNeedSubList.contains(userQuestionnaireRequest.getType())) {
            if (!subTypeList.contains(userQuestionnaireRequest.getSubType())) {
                throw new ServiceException("subType参数错误");
            }
        } else {
            userQuestionnaireRequest.setSubType(0);
        }
        UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(userId, userQuestionnaireRequest.getType(), userQuestionnaireRequest.getSubType());
        if (userInfo != null) {
            //如果已经填写那就直接更新
            userInfo.setContent(userQuestionnaireRequest.getContent());
            userInfoMapper.updateByUserIdAndType(userInfo);
        } else {
            userInfo = new UserInfo();
            userInfo.setUserId(userId);
            userInfo.setType(userQuestionnaireRequest.getType());

            userInfo.setSubType(userQuestionnaireRequest.getSubType());
            userInfo.setContent(userQuestionnaireRequest.getContent());
            userInfo.setCreateTime(LocalDateTime.now());
            userInfoMapper.insertUserInfo(userInfo);
        }
        //更新体脂率和手表sn以及手术时间
        //更新手表sn
        if (4 == userQuestionnaireRequest.getType() && 1 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String sn = (String) map.get("quest1_5");
            if (!StrUtil.isBlank(sn)) {
                User user = new User();
                user.setId(userId);
                user.setSn(sn);
                userMapper.updateUser(user);
            }
        }
        //更新手术时间
        if (4 == userQuestionnaireRequest.getType() && 2 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String operationTime = (String)map.get("quest2_6");
            if (!StrUtil.isBlank(operationTime)) {
                User user = new User();
                user.setId(userId);
                user.setOperationTime(LocalDateTimeUtil.parse(operationTime,"yyyy-MM-dd"));
                userMapper.updateUser(user);
            }
        }
        //更新体脂率
        if (4 == userQuestionnaireRequest.getType() && 4 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String fatPercent = (String) map.get("quest4_6");
            if (!StrUtil.isBlank(fatPercent)) {
                User user = new User();
                user.setId(userId);
                user.setFatPercent( new BigDecimal(fatPercent));
                userMapper.updateUser(user);
            }
        }

        return 1;
    }

    @Override
    public UserInfo selectByUserIdAndSubType(int userId, int type, int subType) {
        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> typeNeedSubList = asList(2,4,6,8,10,12,14,16);
        if (typeNeedSubList.contains(type))  {
            if (!subTypeList.contains(subType)) {
                throw new ServiceException("subType参数错误");
            }
        } else {
            subType = 0;
        }
        return userInfoMapper.selectByUserIdAndSubType(userId, type, subType);
    }

    @Override
    @Transactional
    public Integer updateByUserIdAndType(int userId, int id, UserQuestionnaireRequest userQuestionnaireRequest) throws JsonProcessingException {
        if (userQuestionnaireRequest.getSubType() == null) {
            userQuestionnaireRequest.setSubType(0);
        }
        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> typeNeedSubList = asList(2,4,6,8,10,12,14,16);
        if (typeNeedSubList.contains(userQuestionnaireRequest.getType()))  {
            if (!subTypeList.contains(userQuestionnaireRequest.getSubType())) {
                throw new ServiceException("subType参数错误");
            }
        } else {
            userQuestionnaireRequest.setSubType(0);
        }
        if (1 == userQuestionnaireRequest.getType()) {
            return 1;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setUserId(userId);
        userInfo.setType(userQuestionnaireRequest.getType());
        userInfo.setSubType(userQuestionnaireRequest.getSubType());
        userInfo.setContent(userQuestionnaireRequest.getContent());
        userInfo.setUpdateTime(LocalDateTime.now());
        userInfoMapper.updateByUserIdAndType(userInfo);

        //更新体脂率和手表sn
        //更新手表sn
        if (4 == userQuestionnaireRequest.getType() && 1 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String sn = (String) map.get("quest1_5");
            if (!StrUtil.isBlank(sn)) {
                User user = new User();
                user.setId(userId);
                user.setSn(sn);
                userMapper.updateUser(user);
            }
        }
        //更新手术时间
        if (4 == userQuestionnaireRequest.getType() && 2 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String operationTime = (String)map.get("quest2_6");
            if (!StrUtil.isBlank(operationTime)) {
                User user = new User();
                user.setId(userId);
                user.setOperationTime(LocalDateTimeUtil.parse(operationTime,"yyyy-MM-dd"));
                userMapper.updateUser(user);
            }
        }
        //更新体脂率
        if (4 == userQuestionnaireRequest.getType() && 4 == userQuestionnaireRequest.getSubType()) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userQuestionnaireRequest.getContent(), Map.class);
            String fatPercent = (String) map.get("quest4_6");
            if (!StrUtil.isBlank(fatPercent)) {
                User user = new User();
                user.setId(userId);
                user.setFatPercent( new BigDecimal(fatPercent));
                userMapper.updateUser(user);
            }
        }
        return 1;
    }

    @Override
    public int deleteByIds(Integer[] ids, Integer userId) {
        return userInfoMapper.deleteByIds(ids, userId);
    }

    @Override
    public int deleteById(Integer id, Integer userId) {
        return userInfoMapper.deleteById(id, userId);
    }

    @Override
    public List<QuestionContentVo> selectContentsByType(Integer type) {
        List<QuestionContentVo> list = userInfoMapper.selectContentsByType(type);
        return list;
    }
}
